package edu.pku.sei.transformation.editor.parser.texttemplate.element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TextElementRegister {
	private StructuredTextElement root = new StructuredTextElement();
	private HashMap<String, TextElement> namedElementMap = new HashMap<String, TextElement>();
	private ArrayList<ElementEdge> edges = new ArrayList<ElementEdge>();
	private ArrayList<TextElement> elements = new ArrayList<TextElement>();
	
	public TextElementRegister() {
		root.setName("root");
		root.setRegister(this);
	}
	public TextElement getElement(String name, boolean isVariable) {
		TextElement result = namedElementMap.get(name);
		return result;
	}
	
	public List<TextElement> getAllElements() {
		return this.elements;
	}
	
	public List<ElementEdge> getAllEdges() {
		return this.edges;
	}
	
	public void addElement(Object element) {
		if (element instanceof TextElement) {
			elements.add((TextElement) element);
			if (element instanceof StructuredTextElement
					|| element instanceof VariableTextElement) {
				namedElementMap.put(((TextElement)element).getName(), (TextElement) element);
			}
			((TextElement) element).setRegister(this);
		} else if (element instanceof ElementEdge) {
			edges.add((ElementEdge) element);
		}
	}
	
	public void setRoot(List<TextElement> list) {
		root.addSubElements(list, false);
	}
	
	public StructuredTextElement getRoot() {
		return root;
	}
	
	@Override
	public String toString() {
		String s = "";
		for (TextElement element : elements) {
			s += element.toString() + "\n";
		}
		return s;
	}
}
